package com.tjise.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjise.eduservice.entity.EduChapter;
import com.tjise.eduservice.entity.EduVideo;
import com.tjise.eduservice.entity.chapter.ChapterVo;
import com.tjise.eduservice.entity.chapter.VideoVo;
import com.tjise.eduservice.mapper.EduChapterMapper;
import com.tjise.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjise.eduservice.service.EduVideoService;
import com.tjise.servicebase.exceptionhandler.TjiseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author TJISE
 * @since 2021-06-23
 */
@Service
@Slf4j
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 根据课程id查询课程大纲
     *
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> getChapterVoByCourseId(String courseId) {
        //1根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(chapterQueryWrapper);
        //2根据课程id查询课程里面所有的小节
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(videoQueryWrapper);
        //创建list集合进行最终封装
        ArrayList<ChapterVo> finalList = new ArrayList<>();
        //3遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

            ArrayList<VideoVo> finalVideoVoList = new ArrayList<>();
            //4遍历查询小节list集合进行封装
            for (int j = 0; j < eduVideoList.size(); j++) {
                EduVideo eduVideo = eduVideoList.get(j);
                if (eduVideo.getChapterId().equals(chapterVo.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    finalVideoVoList.add(videoVo);
                }
            }

            chapterVo.setChildren(finalVideoVoList);
        }
        log.info("-------------------EduChapterServiceImpl---------类中的getChapterVoByCourseId()最后返回的值为" + finalList);


        return finalList;
    }

    /**
     * 删除章节的方法
     *
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        // 判断章节下是否有小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper();
        // 根据chapterid章节id，去查询小节表
        wrapper.eq("chapter_id", chapterId);
        // 查询出来的结果
        int count = eduVideoService.count(wrapper);
        log.info("--------------EduChapterServiceImpl--中的deleteChapter()方法查询的记录（小节）--------" + count);
        // 判断如果 count有数据就不删除
        if (count > 0) {
            // 不进行删除
            throw new TjiseException(20001, "不能删除");
        } else {
            log.info("chapterId的值为：" + chapterId);
            // 进行删除
            int result = baseMapper.deleteById(chapterId);
            log.info("----------------EduChapterServiceImpl中的deleteChapter()方法的result值为：----------------" + result);
            // 判断是否大于0 ，大于0 就是失败
            return result > 0;
        }
    }

    /**
     * 根据课程id删除章节
     *
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        // 创建查询条件
        wrapper.eq("course_id", courseId);
        // 进行删除
        int delete = baseMapper.delete(wrapper);
        log.info("------------------EduChapterServiceImpl类中的removeChapterByCourseId() 删除后的影响结果为：-----------------------" + delete);
    }
}
